﻿using Authority.Logic;
using Authority.Model;
using Ioc.Library;
using Microsoft.AspNetCore.Mvc;
using Network.Library;
using System;
using Tool.Model;

namespace Web.Manage.Areas.Admin.Controllers
{
    /// <summary>
    /// 登录控制器
    /// </summary>
    [Area("Admin")]
    public class LoginController : BaseController
    {
        /// <summary>
        /// 登录界面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取验证码
        /// </summary>
        /// <returns></returns>
        public FileContentResult GetValidateCode()
        {
            var resultInfo = new ResultFileContent();
            try
            {
                ISysUserService sysUser = UnityCIContainer.Instance.GetService<ISysUserService>();
                resultInfo = sysUser.LoadValidateCode();
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnFile(resultInfo);
        }

        ///</summary>
        ///登录
        ///</summary>
        public ContentResult LoginCheck()
        {
            var resultInfo = new ResultJsonInfo<SysUserInfo>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<LoginRequest>>();

                ISysUserService sysUser = UnityCIContainer.Instance.GetService<ISysUserService>();

                resultInfo = sysUser.UserLogin(parametersInfo.parameters);

                //验证通过
                if (resultInfo != null && resultInfo.Code == ActionCodes.Success)
                {
                    if (resultInfo.Success)
                    {
                        UserSession.AddLoginUser<SysUserInfo>(resultInfo.Data);
                    }
                }    
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }
    }
}